package com.jetinno.utils;

import android.app.ActivityManager;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.support.v4.content.FileProvider;
import android.util.Log;

import java.io.File;
import java.util.ArrayList;

/**
 * 对系统操作类
 * Created by zhangyuncai on 2021/9/6.
 */
public class SystemUtil {

    private static final String UNI_WIN_HIDE_BAR = "com.allwinner.hide_nav_bar";
    private static final String UNI_WIN_SHOW_BAR = "com.allwinner.show_nav_bar";
    private static final String UNI_WIN_WAKE = "com.allwinner.wake";
    private static final String UNI_WIN_SLEEP = "com.allwinner.sleep";

    /**
     * 检测指定的Service是否处于运行状态
     *
     * @param context
     * @param ServiceName
     * @return
     */
    public static boolean isServiceRunning(Context context, String ServiceName) {
        ActivityManager myManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        ArrayList<ActivityManager.RunningServiceInfo> runningService = (ArrayList<ActivityManager.RunningServiceInfo>) myManager.getRunningServices(1000);
        for (int i = 0; i < runningService.size(); i++) {
            if (runningService.get(i).service.getClassName().toString().equals(ServiceName)) {
                return true;
            }
        }
        return false;
    }

    /**
     * 强制退出整个应用
     *
     * @param context
     */
    public static void forcedOutOfApp(Context context) {
        //读取系统当前版本号
        int currentVersion = Build.VERSION.SDK_INT;
        //android版本高于2.2
        if (currentVersion > Build.VERSION_CODES.ECLAIR_MR1) {
            Intent intent = new Intent(Intent.ACTION_MAIN);
            intent.addCategory(Intent.CATEGORY_HOME);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(intent);
            System.exit(0);
        }
        //android版本低于2.2
        else {
            ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
            am.restartPackage(context.getPackageName());
        }
    }

    public static void restartApp(Context context) {
        Intent intent = context.getPackageManager().getLaunchIntentForPackage(context.getPackageName());
        intent.setAction(Intent.ACTION_MAIN);
        intent.addCategory(Intent.CATEGORY_LAUNCHER);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.putExtra("REBOOT", "reboot");
        context.startActivity(intent);
        android.os.Process.killProcess(android.os.Process.myPid());
    }

    /**
     * 隐藏导航栏
     */
    public static void hideNavigationBar(Context context) {
        Intent intent = new Intent(UNI_WIN_HIDE_BAR);
        context.sendBroadcast(intent);
    }

    /**
     * 显示导航栏
     */
    public static void showNavigationBar(Context context) {
        Intent intent = new Intent(UNI_WIN_SHOW_BAR);
        context.sendBroadcast(intent);
    }

    /**
     * 系统休眠
     *
     * @param context
     */
    public static void sleep(Context context) {
        Intent intent = new Intent(UNI_WIN_SLEEP);
        context.sendBroadcast(intent);
    }

    /**
     * 系统唤醒
     *
     * @param context
     */
    public static void awake(Context context) {
        Intent intent = new Intent(UNI_WIN_WAKE);
        context.sendBroadcast(intent);
    }

    /**
     * 安装APK
     *
     * @param path    安装路径
     * @param context
     */
    public static void installApk(String path, Context context) {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        File apkFile = new File(path);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {//适配7.0以上
            intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            Uri contentUri = FileProvider.getUriForFile(context, context.getPackageName() + ".fileProvider", apkFile);
            intent.setDataAndType(contentUri, "application/vnd.android.package-archive");
        } else {
            intent.setDataAndType(Uri.fromFile(apkFile),
                    "application/vnd.android.package-archive");
        }
        Log.d("install apk=>", path);

        context.startActivity(intent);
    }

    /**
     * 重启系统
     *
     * @param context
     */
    public static void reboot(Context context) {
        Intent intent = new Intent("com.allwinner.reboot");
        context.sendBroadcast(intent);
    }


}
